<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class InitNewJika extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        // 删除旧的集卡
        Schema::disableForeignKeyConstraints();
        Schema::dropIfExists('jika_activity');
        Schema::dropIfExists('jika_box_relations');
        Schema::dropIfExists('jika_card_records');
        Schema::dropIfExists('jika_cards');
        Schema::dropIfExists('jika_remain_times');
        Schema::dropIfExists('jika_skus');
        Schema::dropIfExists('jika_user_cards');

        Schema::create('jika_activity', function (Blueprint $table) {
            $table->comment = '集卡';

            $table->increments('id');
            $table->uuid('uuid')->index() ;

            $table->string('title', 255)->nullable()->comment('标题'); // 标题
            $table->string('sub_title', 255)->nullable()->comment('副标题'); // 副标题

            $table->string('bg', 255)->nullable()->comment('背景图'); // 封面
            $table->string('thumb', 255)->nullable()->comment('封面'); // 封面
            $table->json('page')->nullable()->comment('详情图');


            $table->integer('visit_total')->default(0)->comment('访问人数'); // 访问人数

            $table->integer('stock')->default(0)->comment('剩余库存'); // 剩余库存
            $table->integer('sales')->default(0)->comment('销量'); 


            $table->timestamp('start_at')->nullable()->index()->comment('开始时间');
            $table->timestamp('end_at')->nullable()->index()->comment('结束时间');

            $table->integer('list_weight')->default(100)->index()->comment('权重');
            $table->tinyInteger('status')->index()->default(1)->nullable()->comment('0已下架 1为已上架');


            $table->softDeletes();
            $table->timestamps();
        });

        Schema::create('jika_tasks', function (Blueprint $table) {
            $table->comment = '集卡';

            $table->increments('id');
            $table->uuid('uuid')->index() ;
            $table->integer('activity_id')->index()->nullable()->comment('活动id');

            $table->string('title', 255)->nullable()->comment('标题'); // 标题
            $table->string('sub_title', 255)->nullable()->comment('副标题'); // 副标题

            $table->json('chip_ids')->nullable()->comment('碎片id'); // 副标题

            $table->integer('stock')->default(0)->comment('奖品库存'); // 销量
            $table->integer('sales')->default(0)->comment('奖品销量'); // 销量

            $table->integer('swap_limit')->default(0)->comment('每人限制兑换次数'); // 销量

            $table->json('link')->nullable()->comment('链接'); // 链接

            $table->integer('list_weight')->default(100)->index()->comment('权重');
            $table->tinyInteger('status')->index()->default(1)->nullable()->comment('0已下架 1为已上架');

            $table->softDeletes();
            $table->timestamps();
        });


        Schema::create('jika_skus', function (Blueprint $table) {
            $table->comment = '集卡可兑换的奖品';

            $table->increments('id');
            $table->uuid('uuid')->index();

            $table->integer('activity_id')->index()->nullable()->comment('活动id');
            $table->integer('task_id')->index()->nullable()->comment('任务id');

            $table->tinyInteger('sku_type')->default(1)->comment('奖品类型, 1为自定义实物，2为绑定商城商品 3为平台虚拟资产  4为无任何奖品');

            $table->json('assets')->nullable()->comment('当sku_type为2时，绑定的虚拟资产'); 
            $table->integer('product_id')->index()->nullable()->comment('当sku_type为1时, 绑定的商品id');

            $table->string('title', 100)->nullable()->comment('标题'); // 标题
            $table->string('number', 255)->nullable()->comment('编号'); // 副标题
            $table->string('thumb', 255)->nullable()->comment('封面'); // 副标题


            $table->integer('money_price')->unsigned()->nullable()->comment('人民币价格');     // 价格
            $table->integer('score_price')->unsigned()->nullable()->comment('积分价格');     // 价格

            $table->integer('stock')->default(0)->comment('奖品库存'); // 销量
            $table->integer('sales')->default(0)->comment('奖品销量'); // 销量

            // $table->float('odds')->nullable()->comment('中奖几率, 30表示30%');

            $table->integer('level')->default(1)->index()->comment('sku等级，一共5个等级');

            $table->integer('list_weight')->default(100)->index()->comment('权重');
            $table->tinyInteger('status')->index()->default(1)->nullable()->comment('0已下架 1为已上架');

            $table->softDeletes();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::disableForeignKeyConstraints();
        Schema::dropIfExists('jika_activity');
        Schema::dropIfExists('jika_skus');
        Schema::dropIfExists('jika_tasks');
    }
}
